package com.shop2cn.iapi.pretrade.testcase;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.pretrade.paremeter.MultiDeliveryReq;
import com.shop2cn.iapi.pretrade.paremeter.pojo.DeliveryPkgVo;
import com.shop2cn.iapi.pretrade.paremeter.pojo.DeliverySegmentVo;
import com.shop2cn.iapi.pretrade.service.MultiDeliveryCall;
import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.SQPayGateWayCallService;
import com.ymttest.business.service.shenqi.DGSQBuildProxyChainCallService;
import com.ymttest.business.service.shenqi.SqTradingCallService;
import com.ymttest.business.service.shenqi.bean.ShenqiUserSet;
import com.ymttest.database.sharding.db.ext.model.DeliveryPo;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.database.sharding.db.sqlwapper.OrderWapper;
import com.ymttest.utils.TestDataManager;

/**
 * 自动确认收货提醒通知FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_MultiDelivery {

	private static MultiDeliveryReq multiDeliveryReq = new MultiDeliveryReq();
	private static MultiDeliveryCall multiDeliveryCall = new MultiDeliveryCall();
	
	private static OrderWapper orderWapper = new OrderWapper();
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
	
	private static long autotest_goodsOwnerId = ShenqiUserSet.autotest_goodsOwnerId;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("神奇订单多段发货");
	}

	@Before
	public void caseUp() {

		multiDeliveryReq = new MultiDeliveryReq();
		multiDeliveryCall = new MultiDeliveryCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(multiDeliveryCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verifyDB(Long orderId, List<DeliveryPkgVo> packages) {
		
		Logger.comment("校验order表");
		
		OrderPo orderPo = orderShardingWapper.selectOrderbyOrderId(orderId);
		
		Logger.verifyEquals(3, orderPo.getOrderStatus(), "验证orderStatus");
		Logger.verifyEquals(true, orderPo.getMultiSegmentDelivery(), "验证MultiSegmentDelivery");
		
		Logger.comment("校验delivery表");
		
		for (int i = 0; i < packages.size(); i++) {
			
			DeliveryPkgVo deliveryPkgVo = packages.get(i);
			
			List<DeliverySegmentVo> segments = deliveryPkgVo.getSegments();
			
			for (int j = 0; j < segments.size(); j++) {
				
				DeliverySegmentVo deliverySegmentVo = segments.get(j);
				
				DeliveryPo deliveryPo = orderWapper
						.selectDeliveryByOrderIdAndBillCode(
								orderId, deliverySegmentVo.getBillNo());
				
				Logger.verifyEquals(deliverySegmentVo.getBillNo(),
						deliveryPo.getBillNo(), "验证BillNo");
				Logger.verifyEquals(deliverySegmentVo.getYmtExpressCode(),
						deliveryPo.getYmtExpressCode(), "验证YmtExpressCode");
				Logger.verifyEquals(multiDeliveryReq.getDeliverySource(),
						deliveryPo.getSource(), "验证Source");
				Logger.verifyEquals(i, deliveryPo.getPkgIndex(), "验证PkgIndex");
				Logger.verifyEquals(j, deliveryPo.getSegmentIndex(),
						"验证SegmentIndex");
			}
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_MultiDelivery_001() {
		Logger.start(true, "订单接单后，货头发货，多段发货");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean, BigDecimal.ZERO);

			long userId = placeOrderResp.getUserId();
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();
			
			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					(int)userId, BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);
			
			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			
			Logger.comment("前置数据准备结束");
					
			// 物流单号
			String DeliverSummary = TestDataManager.randomStr(10);
			
			DeliverySegmentVo deliverySegmentVo = new DeliverySegmentVo();
			
			deliverySegmentVo.setBillNo(DeliverSummary);
			deliverySegmentVo.setExpressName("自动化测试物流");
			deliverySegmentVo.setMobile(TestDataManager.getPhoneNum());
			deliverySegmentVo.setYmtExpressCode("Y022");
			
			DeliveryPkgVo deliveryPkgVo = new DeliveryPkgVo();
			
			deliveryPkgVo.setSegments(Arrays.asList(deliverySegmentVo));
			
			multiDeliveryReq.setPackages(Arrays.asList(deliveryPkgVo));

			multiDeliveryReq.setCdId(1234L);
			multiDeliveryReq.setGoodsOwnerId(sellerId);
			multiDeliveryReq.setDeliverySource(11);
			multiDeliveryReq.setOrderId(orderId);

			multiDeliveryCall.setData(multiDeliveryReq);
			multiDeliveryCall.callService();

			Logger.verifyEquals(true, multiDeliveryCall.getSuccess(),
					"验证Success");
			
			Thread.sleep(5000);
			
			this.verifyDB(orderId, multiDeliveryReq.getPackages());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_MultiDelivery_002() {
		Logger.start(true, "订单接单后，货头发货，多段多包裹发货");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean, BigDecimal.ZERO);

			long userId = placeOrderResp.getUserId();
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();
			
			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					(int)userId, BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);
			
			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			
			Logger.comment("前置数据准备结束");
					
			// 物流单号
			String deliverSummary1 = TestDataManager.randomStr(10);

			DeliverySegmentVo deliverySegmentVo1 = new DeliverySegmentVo();

			deliverySegmentVo1.setBillNo(deliverSummary1);
			deliverySegmentVo1.setExpressName("自动化测试物流");
			deliverySegmentVo1.setMobile(TestDataManager.getPhoneNum());
			deliverySegmentVo1.setYmtExpressCode("Y022");
			
			// 物流单号
			String deliverSummary2 = TestDataManager.randomStr(10);

			DeliverySegmentVo deliverySegmentVo2 = new DeliverySegmentVo();

			deliverySegmentVo2.setBillNo(deliverSummary2);
			deliverySegmentVo2.setExpressName("自动化测试物流");
			deliverySegmentVo2.setMobile(TestDataManager.getPhoneNum());
			deliverySegmentVo2.setYmtExpressCode("Y022");

			DeliveryPkgVo deliveryPkgVo1 = new DeliveryPkgVo();

			deliveryPkgVo1.setSegments(Arrays.asList(deliverySegmentVo1, deliverySegmentVo2));
			
			// 物流单号
			String deliverSummary3 = TestDataManager.randomStr(10);

			DeliverySegmentVo deliverySegmentVo3 = new DeliverySegmentVo();

			deliverySegmentVo3.setBillNo(deliverSummary3);
			deliverySegmentVo3.setExpressName("自动化测试物流");
			deliverySegmentVo3.setMobile(TestDataManager.getPhoneNum());
			deliverySegmentVo3.setYmtExpressCode("Y022");

			// 物流单号
			String deliverSummary4 = TestDataManager.randomStr(10);

			DeliverySegmentVo deliverySegmentVo4 = new DeliverySegmentVo();

			deliverySegmentVo4.setBillNo(deliverSummary4);
			deliverySegmentVo4.setExpressName("自动化测试物流");
			deliverySegmentVo4.setMobile(TestDataManager.getPhoneNum());
			deliverySegmentVo4.setYmtExpressCode("Y022");

			DeliveryPkgVo deliveryPkgVo2 = new DeliveryPkgVo();

			deliveryPkgVo2.setSegments(Arrays.asList(deliverySegmentVo3,
					deliverySegmentVo4));

			multiDeliveryReq.setPackages(Arrays.asList(deliveryPkgVo1, deliveryPkgVo2));

			multiDeliveryReq.setCdId(1234L);
			multiDeliveryReq.setGoodsOwnerId(sellerId);
			multiDeliveryReq.setDeliverySource(11);
			multiDeliveryReq.setOrderId(orderId);

			multiDeliveryCall.setData(multiDeliveryReq);
			multiDeliveryCall.callService();

			Logger.verifyEquals(true, multiDeliveryCall.getSuccess(),
					"验证Success");

			Thread.sleep(5000);

			this.verifyDB(orderId, multiDeliveryReq.getPackages());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_MultiDelivery_003() {
		Logger.start(true, "订单接单后，货头发货，一个订单多段发货多次");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean, BigDecimal.ZERO);

			long userId = placeOrderResp.getUserId();
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();
			
			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					(int)userId, BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);
			
			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			
			String deliverSummary1 = TestDataManager.randomStr(10);
			
			DeliverySegmentVo deliverySegmentVo1 = new DeliverySegmentVo();
			
			deliverySegmentVo1.setBillNo(deliverSummary1);
			deliverySegmentVo1.setExpressName("自动化测试物流");
			deliverySegmentVo1.setMobile(TestDataManager.getPhoneNum());
			deliverySegmentVo1.setYmtExpressCode("Y022");
			
			String deliverSummary2 = TestDataManager.randomStr(10);
			
			DeliverySegmentVo deliverySegmentVo2 = new DeliverySegmentVo();
			
			deliverySegmentVo2.setBillNo(deliverSummary2);
			deliverySegmentVo2.setExpressName("自动化测试物流");
			deliverySegmentVo2.setMobile(TestDataManager.getPhoneNum());
			deliverySegmentVo2.setYmtExpressCode("Y022");
			
			DeliveryPkgVo deliveryPkgVo = new DeliveryPkgVo();
			
			deliveryPkgVo.setSegments(Arrays.asList(deliverySegmentVo1, deliverySegmentVo2));
			
			multiDeliveryReq.setPackages(Arrays.asList(deliveryPkgVo));

			multiDeliveryReq.setCdId(1234L);
			multiDeliveryReq.setGoodsOwnerId(sellerId);
			multiDeliveryReq.setDeliverySource(11);
			multiDeliveryReq.setOrderId(orderId);

			multiDeliveryCall.setData(multiDeliveryReq);
			multiDeliveryCall.callService();
			
			// 再次发货
			String deliverSummary3 = TestDataManager.randomStr(10);
			
			DeliverySegmentVo deliverySegmentVo3 = new DeliverySegmentVo();
			
			deliverySegmentVo3.setBillNo(deliverSummary3);
			deliverySegmentVo3.setExpressName("自动化测试物流");
			deliverySegmentVo3.setMobile(TestDataManager.getPhoneNum());
			deliverySegmentVo3.setYmtExpressCode("Y022");
			
			DeliveryPkgVo deliveryPkgVo2 = new DeliveryPkgVo();
			
			deliveryPkgVo2.setSegments(Arrays.asList(deliverySegmentVo3));
			
			multiDeliveryReq.setPackages(Arrays.asList(deliveryPkgVo2));
			
			multiDeliveryCall.setData(multiDeliveryReq);
			multiDeliveryCall.callService();

			Logger.verifyEquals(true, multiDeliveryCall.getSuccess(),
					"验证Success");
			
			Thread.sleep(5000);

			Logger.verifyEquals(true, multiDeliveryCall.getSuccess(), "验证Success");
			Logger.verifyNotNull(
					orderWapper.selectActualDeleteDeliveryByBillNo(deliverySegmentVo1.getBillNo()),
					"验证旧运单号删除成功");
			Logger.verifyNotNull(
					orderWapper.selectActualDeleteDeliveryByBillNo(deliverySegmentVo2.getBillNo()),
					"验证旧运单号删除成功");
			
			this.verifyDB(orderId, multiDeliveryReq.getPackages());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
